<template>
	<view>asdadasdasdadasasdasdasdasdasdasdasdasd</view>
</template>
<script>
	export default {
		props: {
			lat: {
				type: Number,
				required: true
			},
			lng: {
				type: Number,
				required: true
			}
		},
		data() {
			return {
				distance: ''
			}
		},
		created() {
			this.getLocation()
		},
		methods: {
			// 获取当前位置并计算距离
			getLocation() {
				uni.getLocation({
					type: 'gcj02',
					success: res => {
						this.calculateDistance(res.latitude, res.longitude)
					},
					fail: err => {
						console.error('getLocation failed', err)
					}
				})
			},
			// 计算距离
			calculateDistance(lat2, lng2) {
				const radLat1 = this.getRadian(this.lat)
				const radLat2 = this.getRadian(lat2)
				const a = radLat1 - radLat2
				const b = this.getRadian(this.lng) - this.getRadian(lng2)
				const s =
					2 *
					Math.asin(
						Math.sqrt(
							Math.pow(Math.sin(a / 2), 2) +
							Math.cos(radLat1) *
							Math.cos(radLat2) *
							Math.pow(Math.sin(b / 2), 2)
						)
					)
				const distance = (s * 6378.137).toFixed(2)
				this.distance = distance + ' km'
			},
			// 获取弧度
			getRadian(d) {
				return (d * Math.PI) / 180.0
			}
		}
	}
</script>
<style>

</style>